<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('流程疏导') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('审批人')" prop="assignee">
        <UserSelect
          v-model="queryParams.assignee"
          :placeholder="$t('选择用户')"
        />
      </el-form-item>

      <el-form-item :label="$t('待办标题')" prop="title">
        <el-input
          v-model="queryParams.title"
          :placeholder="$t('待办标题')"
          size="small"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item :label="$t('流程名称')" prop="processName">
        <el-input
          v-model="queryParams.processName"
          size="small"
          :placeholder="$t('流程名称')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="channelBtn"
          @click="openChannelTask"
        >{{ $t('分配新用户') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="adjustNodeBtn"
          @click="getActivityList"
        >{{ $t('调整节点') }}
        </el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      tooltip-effect="darkTable"
      :data="todoList"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40" align="center" />
      <el-table-column :label="$t('待办标题')" align="left" prop="title" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <i v-show="scope.row.urgent === 'Y'" class="urgent" />
          <el-link type="primary" @click="openTask(scope.row)">{{ scope.row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请人')" align="center" prop="applyNickName" width="100" />
      <el-table-column :label="$t('申请时间')" align="center" prop="startTime" class-name="small-padding fixed-width" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('当前环节')" align="center" prop="taskName" class-name="small-padding fixed-width" width="130">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.taskName) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('流程名称')" align="center" prop="processName" class-name="small-padding fixed-width" width="130">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.processName) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :visible.sync="formOpen" :title="$t('流程详情')" :destroy-on-close="true" custom-class="dialogForm" style=" text-align: left;" append-to-body :close-on-click-modal="false" :lock-scroll="false">
      <ProcessInfoForm ref="processInfoForm" />
    </el-dialog>

    <el-dialog :visible.sync="targetUserForm" :title="$t('流程疏导')" width="400px" append-to-body :close-on-click-modal="false" :lock-scroll="false">
      <div v-loading="loading">
        <el-form ref="form" :model="submitData" :rules="rules" class="dataForm">
          <el-row>
            <el-col :span="24">
              <el-form-item :label="$t('将任务分配至用户')" prop="targetUser">
                <UserSelect v-model="submitData.targetUser" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align: right;">
          <el-button @click="cancel">{{ $t("取消") }}</el-button>
          <el-button type="primary" @click="submitForm">{{ $t("确定") }}</el-button>
        </div>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="adjustNodeForm" :title="$t('调整节点')" width="400px" append-to-body :close-on-click-modal="false" :lock-scroll="false">
      <div v-loading="adjustNodeLoading">
        <el-form ref="adjustNodeForm" :model="adjustNodeFormData" :rules="adjustNodeRules" class="dataForm">
          <el-row>
            <el-col :span="24">
              <el-form-item :label="$t('将任务跳转至节点')" prop="adjustNode">
                <el-select v-model="adjustNodeFormData.adjustNode" :placeholder="$t('选择节点')">
                  <el-option
                    v-for="activityVo in activityVoList"
                    :key="activityVo.id"
                    :label="activityVo.name"
                    :value="activityVo.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align: right;">
          <el-button @click="cancel">{{ $t("取消") }}</el-button>
          <el-button type="primary" @click="submitAdjustNode">{{ $t("确定") }}</el-button>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { channelList, channelTask, getActivityList, submitAdjustNode, submitAdjustNodeCheck } from '@/api/act/task/actChannel'
import ProcessInfoForm from '@/views/act/process/processInfoForm'
export default {
  components: {
    ProcessInfoForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 调整节点遮罩层
      adjustNodeLoading: false,
      // 流程疏导按钮是不是禁用
      channelBtn: true,
      // 调整节点按钮是不是禁用
      adjustNodeBtn: true,
      // 总条数
      total: 0,
      // 待办表格数据
      todoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      // 分配用户页面是否展示
      targetUserForm: false,
      // 调整节点页面是否展示
      adjustNodeForm: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        applyUserId: '',
        processName: undefined
      },
      // 需要向后台提交的数据
      submitData: {
        tasks: [],
        targetUser: undefined
      },
      // 流程节点集合
      activityVoList: [],
      // 调整至节点
      adjustNodeFormData: {
        adjustNode: undefined
      },
      rules: {
        targetUser: [
          { required: true, message: this.$t('必填信息'), trigger: ['blur'] }
        ]
      },
      adjustNodeRules: {
        adjustNode: [
          { required: true, message: this.$t('必填信息'), trigger: ['blur'] }
        ]
      },
      wfMenuProcessData: {},
      // 表单参数
      form: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询待办列表 */
    getList() {
      this.loading = true
      channelList(this.queryParams).then(response => {
        this.todoList = response.rows
        this.total = response.total
        this.loading = false
        this.targetUserForm = false
      })
    },
    // 取消按钮
    cancel() {
      this.formOpen = false
      this.targetUserForm = false
      this.adjustNodeForm = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 打开待办页面
    openTask(row) {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.processInfoForm.init(row)
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.submitData.tasks = selection.map(item => item.id)
      this.channelBtn = !(selection.length > 0)
      this.adjustNodeBtn = !(selection.length === 1)
    },
    // 疏导待办
    openChannelTask() {
      this.targetUserForm = true
    },
    // 提交选择的疏导
    submitForm() {
      this.$refs['form'].validate(valid => {
        // 验证数据
        if (valid) {
          channelTask(this.submitData).then(() => {
            this.targetUserForm = false
            this.msgSuccess(this.$t('疏导成功'))
            this.submitData.targetUser = undefined
            this.getList()
          })
        }
      })
    },
    // 重新分配节点选择
    getActivityList() {
      submitAdjustNodeCheck(this.submitData.tasks[0]).then(response => {
        this.adjustNodeFormData.adjustNode = undefined
        if (response.data) {
          this.$alert(this.$t(response.data), this.$t('提示信息'), {
            dangerouslyUseHTMLString: true
          })
        } else {
          this.adjustNodeForm = true
          getActivityList(this.submitData.tasks[0]).then(response => {
            this.activityVoList = response.data
          })
        }
      })
    },
    // 重新分配节点
    submitAdjustNode() {
      this.$refs['adjustNodeForm'].validate(valid => {
        // 验证数据
        if (valid) {
          this.adjustNodeLoading = true
          submitAdjustNode(this.submitData.tasks[0], this.adjustNodeFormData.adjustNode).then(() => {
            this.adjustNodeLoading = false
            this.adjustNodeForm = false
            this.msgSuccess(this.$t('操作成功'))
            this.getList()
          })
        }
      })
    }

  }
}
</script>
